package org.example.cake.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.example.cake.common.Rep;
import org.example.cake.entity.Discount;
import org.example.cake.entity.Userdiscount;
import org.example.cake.service.DiscountService;
import org.example.cake.service.UserdiscountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/discount")
public class DiscountController {

    @Autowired
    private DiscountService discountService;
    @Autowired
    private UserdiscountService userdiscountService;

    /**
     * 后台分页查询所有优惠券
     * @return 优惠券集合
     */
    @GetMapping("/selectAllDiscountEnd")
    public Rep selectAllDiscount(@RequestParam(defaultValue = "1")int page,
                                 @RequestParam(required = false) String discountname,
                                 @RequestParam(required = false) Integer status){
        PageHelper.startPage(page,5);
        List<Discount> discountList = discountService.selectAllDiscount(discountname,status);
        PageInfo<Discount>discountPageInfo=new PageInfo<>(discountList);
        return new Rep().addData("discountList",discountPageInfo.getList()).addData("total",discountPageInfo.getTotal());
    }
    /**
     * 通过userid查询所拥有的优惠券
     * @param userid 用户id
     * @return 优惠券集合
     */
    @GetMapping("/selectDiscountByUserid")
    public Rep selectDiscountByUserid(@RequestParam("userid") Integer userid){
        List<Discount> discountByUserid = discountService.selectDiscountByUserid(userid);
        return new Rep().addData("discountByUserid", discountByUserid);
    }

    /**
     * //新增后台优惠券信息
     * @author 徐
     * @param discount 优惠券对象
     * @date 2025-08-01
     * @return 受影响的行数
     */
    @PostMapping("/insertDiscountEnd")
    public String insertDiscountEnd(@RequestBody Discount discount) {
        return discountService.insertDiscountEnd(discount);
    }

    /**
     * //后台优惠券删除（根据id删除）
     * @author 徐
     * @param discountid 优惠券id
     * @date 2025-08-01
     * @return 优惠券id
     */
    @PostMapping("/deleteDiscountEnd")
    public String deleteDiscountEnd(@RequestParam("discountid") Integer discountid) {
        return discountService.deleteDiscountEnd(discountid);
    }

    /**
     * //修改后台优惠券信息
     * @author 徐
     * @param discount 优惠券对象
     * @date 2025-08-01
     * @return 受影响的行数
     */
    @PostMapping("/updateDiscountEnd")
    public String updateDiscountEnd(@RequestBody Discount discount) {
        return discountService.updateDiscountEnd(discount);
    }
    /**
     * 第一次登录得到的优惠券
     * @return 优惠券集合
     */
    @GetMapping("selectTwoDiscount")
    public Rep selectTwoDiscount(){
        List<Discount> selectTwoDiscount = discountService.selectTwoDiscount();
        return new Rep().addData("selectTwoDiscount",selectTwoDiscount);
    }


    /**
     * 获取节日优惠券
     * @param userdiscount 优惠券id 用户id
     * @return
     */
    @PostMapping("selectifdiscount")
    public  Rep selectifdiscount(@RequestBody Userdiscount userdiscount) {
        boolean flag=false;
        Discount discount = discountService.getyouhuiquan(userdiscount.getDiscountid());
        //根据用户id查询该用户是否以及领取过该优惠券
        Integer res=userdiscountService.getUsercountifTrue(userdiscount);
        if(res>0){
            flag=true;
        }
        return new Rep().addData("discount",discount).addData("flag",flag);
    }

}
